package com.cn;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.monitor.FileAlterationListener;
import org.apache.commons.io.monitor.FileAlterationObserver;
import org.springframework.stereotype.Component;

import java.io.File;

/**
 * @author cm
 */
@Slf4j
@Component
public class FileListener implements FileAlterationListener {


    @Override
    public void onStart(FileAlterationObserver fileAlterationObserver) {
        File directory = fileAlterationObserver.getDirectory();
        log.info("开始监听..." + directory);
    }

    @Override
    public void onDirectoryCreate(File file) {
        log.info("目录被创建了...");
    }

    @Override
    public void onDirectoryChange(File file) {
        log.info("目录被修改了...");
    }

    @Override
    public void onDirectoryDelete(File file) {
        log.info("目录被删除了...");
    }

    @Override
    public void onFileCreate(File file) {
        log.info("文件被创建了..."+file.getName()+"，文件目录:"+file.getAbsolutePath());
    }

    @Override
    public void onFileChange(File file) {
        log.info("文件被修改了...");
    }

    @Override
    public void onFileDelete(File file) {
        log.info("文件被删除了...");
    }

    @Override
    public void onStop(FileAlterationObserver fileAlterationObserver) {
        File directory = fileAlterationObserver.getDirectory();
        log.info("结束监听..." + directory);
    }
}
